package com.xtravel.common.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ProgressBar;

public class HorizontalProgressBar extends ProgressBar {
    private Paint mTextPaint;
    private String mProgressText;

    public HorizontalProgressBar(Context context) {
        super(context);
        System.out.println("1");
        init();
    }

    public HorizontalProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        System.out.println("2");
        init();
    }


    public HorizontalProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        System.out.println("3");
        init();
    }

    private void init() {
        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(18f);
    }

    @Override
    public synchronized void setProgress(int progress) {
        super.setProgress(progress);
        mProgressText = String.valueOf(progress) + '%';
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect = new Rect();
        mTextPaint.getTextBounds(mProgressText, 0, mProgressText.length(), rect);
        int x = (int) (getWidth() * (getProgress() * 1.0 / 100)) - rect.width() - 12;
        int y = (getHeight() / 2) - rect.centerY();
        if (x < rect.width() / 2 + 6) {
            x = 6;
        }
        canvas.drawText(mProgressText, x, y, mTextPaint);
    }
}
